//
//  HCRegisteredView.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/1/22.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCRegisteredView.h"

#import "WZLabel.h"

@interface HCRegisteredView () {
    UITextField *phone;
    UITextField *sms;
    UITextField *pwd;
    UIButton *getCodeButton;
}

@property(nonatomic, strong) dispatch_source_t timer;


@end

@implementation HCRegisteredView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initializeSubview:frame];
    }
    return self;
}

-(void)initializeSubview:(CGRect)frame {
    self.backgroundColor = YBKRGBCOLOR(242, 242, 242, 1);
    
    UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, YBKAdaptiveH(10), frame.size.width, YBKAdaptiveH(96))];
    bg.backgroundColor = [UIColor whiteColor];
    [self addSubview:bg];
    
    phone = [[UITextField alloc] initWithFrame:CGRectMake(YBKAdaptiveW(10), YBKAdaptiveH(3), frame.size.width - YBKAdaptiveW(10), YBKAdaptiveH(48))];
    phone.placeholder = @"输入11位手机号码";
    [phone setValue:[UIFont boldSystemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    [phone setValue:YBKRGBCOLOR(242, 242, 242, 1) forKeyPath:@"_placeholderLabel.textColor"];
    [bg addSubview:phone];
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(YBKAdaptiveW(10), phone.frame.size.height, phone.frame.size.width, 1)];
    line1.backgroundColor = YBKRGBCOLOR(242, 242, 242, 1);
    [bg addSubview:line1];
    
    
    //获取验证码按钮
    getCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    getCodeButton.backgroundColor = YBKRGBCOLOR(240, 96, 96, 1);
    [getCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    getCodeButton.titleLabel.font = YBKSystemFont(YBKAdaptiveFont(12));
    getCodeButton.frame = CGRectMake(frame.size.width - YBKAdaptiveW(115), line1.frame.origin.y+line1.frame.size.height, YBKAdaptiveW(115), YBKAdaptiveH(48));
    [getCodeButton addTarget:self action:@selector(getSmsCodeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [bg addSubview:getCodeButton];
    
    //验证码
    sms = [[UITextField alloc] initWithFrame:CGRectMake(YBKAdaptiveW(10), line1.frame.origin.y+line1.frame.size.height+YBKAdaptiveH(3), getCodeButton.frame.origin.x - YBKAdaptiveW(10), YBKAdaptiveH(45))];
    sms.placeholder = @"输入验证码";
    [sms setValue:[UIFont boldSystemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    [sms setValue:YBKRGBCOLOR(242, 242, 242, 1) forKeyPath:@"_placeholderLabel.textColor"];
    [bg addSubview:sms];
    
    //line2
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(YBKAdaptiveW(10), sms.frame.origin.y+sms.frame.size.height, line1.frame.size.width, 1)];
    line2.backgroundColor = line1.backgroundColor;
    [bg addSubview:line2];
    
    pwd = [[UITextField alloc] initWithFrame:CGRectMake(YBKAdaptiveW(10), line2.frame.origin.y+line2.frame.size.height+YBKAdaptiveW(3), frame.size.width - YBKAdaptiveW(10), YBKAdaptiveH(48))];
    pwd.placeholder = @"设置6-16位密码";
    [pwd setValue:[UIFont boldSystemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    [pwd setValue:YBKRGBCOLOR(242, 242, 242, 1) forKeyPath:@"_placeholderLabel.textColor"];
    [bg addSubview:pwd];
    
    //验证按钮
    UIButton *gou = [UIButton buttonWithType:UIButtonTypeCustom];
    [gou setBackgroundImage:[UIImage imageNamed:@"happy_log_righth"] forState:UIControlStateNormal];
    [gou setBackgroundImage:[UIImage imageNamed:@"happy_log_right"] forState:UIControlStateSelected];
    gou.frame = CGRectMake(YBKAdaptiveW(10), bg.frame.size.height+bg.frame.origin.y+YBKAdaptiveW(10), YBKAdaptiveW(21), YBKAdaptiveW(22));
    gou.hidden = YES;
    [gou addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:gou];
    
//    WZLabel *label = [[WZLabel alloc] initWithFrame:CGRectMake(gou.frame.origin.x+gou.frame.size.width+YBKAdaptiveW(10), gou.center.y - YBKAdaptiveW(9), frame.size.width-YBKAdaptiveW(42), YBKAdaptiveW(20))];
//    label.text = @"同意开心哈乐服务协议";
//    label.font = YBKSystemFont(YBKAdaptiveFont(13));
//    label.textColor = [UIColor lightGrayColor];
//    [label setColor:YBKRGBCOLOR(240, 96, 96, 1) childrenString:@"开心哈乐"];
//    [self addSubview:label];
//    
    UIButton *btn_sure = [[UIButton alloc] initWithFrame:CGRectMake((frame.size.width - YBKAdaptiveW(350))/2.0, YBKAdaptiveH(35)+gou.frame.origin.y+gou.frame.size.height, YBKAdaptiveW(350), YBKAdaptiveW(45))];
    [btn_sure setBackgroundImage:[UIImage imageNamed:@"happy_log_sure"] forState:UIControlStateNormal];
    [btn_sure addTarget:self action:@selector(validateCodeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn_sure];
    
}

#pragma mark 验证手机号码
-(void)getSmsCodeBtn:(UIButton *)btn {
    if ((![PublicFunction isNull:phone.text])) {
        if ([ PublicFunction checkTelNumber:phone.text]) {
            
            if ([self.delegate respondsToSelector:@selector(hc_registerGetCode:)]&&self.delegate) {
                [self.delegate hc_registerGetCode:phone.text];
            }
        }else{
            [XHToast showBottomWithText:@"手机号码格式不正确"];
        }
    }
}

- (void)startCountDown {

    [self theWayLessTimeButton:getCodeButton WithTime:60 AndTitle:@"验证码"];
}

- (void)validateCodeAction:(UIButton *)dender {

    if ((![PublicFunction isNull:phone.text])) {
        if ([PublicFunction checkTelNumber:phone.text] && sms.text.length != 0) {
            
            if ([self.delegate respondsToSelector:@selector(hc_registerValidateCode:)]&&self.delegate) {
                [self.delegate hc_registerValidateCode:sms.text];
            }
        }else if (sms.text.length == 0 && [PublicFunction checkTelNumber:phone.text]){
            
            [XHToast showBottomWithText:@"请输入验证码"];
        }
        else {
        
            [XHToast showBottomWithText:@"手机号码格式不正确"];
        }
    }
}

#pragma mark -- 倒计时按钮
-(void)theWayLessTimeButton:(UIButton *)button WithTime:(NSInteger)time AndTitle:(NSString *)title{
    //    UIButton *buttonLess = (UIButton *)button;
    button.titleLabel.numberOfLines = 0;
    __block int timeout= (int)time;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    // 创建一个定时器(dispatch_source_t本质还是个OC对象,创建出来的对象需要强引用)
    
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    // 设置定时器的各种属性(几时开始任务，每隔多长时间执行一次)
    // GCD的时间参数，一般是纳秒(1秒 = 10的9次方纳秒)
    // 何时开始执行第一个任务
    dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)); //比当前时间晚1秒
    //    dispatch_time_t start = DISPATCH_TIME_NOW;// 当前时间执行
    uint64_t interval= 2.0 * NSEC_PER_SEC;
    dispatch_source_set_timer(self.timer, start, interval, 0); // NSEC_PER_SEC 纳秒
    // 设置回调
    dispatch_source_set_event_handler(self.timer, ^{
        
        timeout--;
        //        记好了刷新UI的时候一定要在主队列中刷新,就是主线程.
        dispatch_async(dispatch_get_main_queue(), ^{
            button.userInteractionEnabled=NO;
            [button setTitle:[NSString stringWithFormat:@"   %ds后重新获取   ",timeout] forState:UIControlStateNormal];
            //            NSLog(@"-----%d",timeout);
            if (timeout == 1) {
                
                [button setTitle:@"   获取验证码   " forState:UIControlStateNormal];
                button.userInteractionEnabled=YES;
                dispatch_cancel(self.timer);
                self.timer = nil;
            }
        });
        /*
         GCD的定时器要比NStimer的定时器要精确的多.
         */
    });
    // 启动定时器
    dispatch_resume(self.timer);
}


-(void)btnAction:(UIButton *)btn {
    btn.selected = !btn.selected;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
